//预览自定义网页编辑插件
;(function($){

    $.fn.webeditorPreviewer=function(params,data){
    	params.stage = $(this);
    	params.stage.html("");
		//初始化页面
		initWebPage(params,renderWebPage);
    }
    
    //初始化页面
    function initWebPage(params,callback){
    	$.ajax({
    		url : params.url,
    		type : "post",
    		data : params.data,
    		success : function(data){
    			callback(params,data);
    		}
    	});
    }
    
    //渲染编辑的Web页面
    function renderWebPage(params,data){
    	for(var i = 0; i < data.length; i++){
    		data[i].opts = JSON.parse(data[i].opts);
    	}
    	if(params.scrolling){
    		//放入等待加载序列
    		//获取页面可视高度
    		params.isOver = true//未结束加载;
    		var waitingBox = new Array();//待加载列表
    		var changeScroller = $("#websiteeditor_childchangescroller").val();
    		params.getParentWinHeight = false;
    		if("parent" == changeScroller){
    			params.getParentWinHeight = true;
    		}
    		var winHeight = $(window).height();
    		var scrollTopx = document.body.scrollTop||document.documentElement.scrollTop;
    		var stageWidth = params.stage.width();
    		var isOver = true;
    		
    		//判断是否加载
			var ccHeight = params.getParentWinHeight?$(window.parent).height():winHeight;
			ccHeight = parseInt(ccHeight);
			var parentHeight = ccHeight;
    		for(var i = 0; i < data.length; i++){
    			//如果控件距离顶部距离小于页面高度，或属性为fixed，直接加载，否则进入等待加载序列
    			var boxOpts = data[i];
    			if(checkBoxLoad(ccHeight+scrollTopx,stageWidth,boxOpts)){
    				buildOneBox(params,boxOpts);
    			}else{
    				params.isOver = false;//未结束加载，有东西需要加载
    				isOver = false;
    				waitingBox.push(data[i]);
    			}
        	}
    		
    		//控件加载完毕后渲染页面最大高度params.maxHeight
    		//先删选出初始加载在页面的元素
    		if(!params.getParentWinHeight){
    			$(window).scroll(function(){
                    //scrollTop是浏览器滚动条的top位置，
                    var scrollTop=window.document.body.scrollTop||window.document.documentElement.scrollTop;
                    if(!params.isOver){
                    	var cHeight = winHeight;
                    	cHeight = parseInt(cHeight);
                    	waitingBox = appendBox(params,scrollTop,cHeight,stageWidth,waitingBox);
                    }
                });
    		}else{
    			$("#websiteeditor_childchangescroller").change(function(){
        			var scrollTop=window.parent.document.body.scrollTop||window.parent.document.documentElement.scrollTop;
                    if(!params.isOver){
                    	var cHeight = params.getParentWinHeight?parentHeight:winHeight;
                    	cHeight = parseInt(cHeight);
                    	waitingBox = appendBox(params,scrollTop,cHeight,stageWidth,waitingBox);
                    }
        		});
    			$(window.parent).scroll(function(){
					scrollChange();
	            });
    		}
    	}else{
	    	for(var i = 0; i < data.length; i++){
	    		buildOneBox(params,data[i]);
	    	}
    	}
    }
    
    //在stage放置一个盒子
    function buildOneBox(params,info){
    	var box = $("<div class='pageElements biggerboxs'></div>").deBoxPreviewer(params,info);
    	params.stage.append(box);
    	if("1" == box.data("sfmrdj")){
    		box.click();
    	}
    }
    
    //根据高度和控件数据判断是否加载
    function checkBoxLoad(wheight,stageWidth,info){
		if(checkIsFixed(info)){
			return true;
		}else{
			var top = stageWidth * info.top / 100;
			return wheight > top;
		}
    }
    
    //检查是否fixed
    function  checkIsFixed(info){
    	for(var i = 0; i < info.opts.length; i++){
			if("position" == info.opts[i].text && "fixed" == info.opts[i].content){
				return true;
			}
    	}
    	return false;
    }
    
	//滚动条滚动添加box
    function appendBox(params,scrollTop,winHeight,stageWidth,waitingBox){
   	   //遍历删选出需要加载的元素
       //是否增加加载,如果没有增加加载扩大比例
    	var leftBox = new Array();//剩余待加载列表
    	var isOver = true;
     	var buildBoxs = new Array();
	   	for(var i = 0; i < waitingBox.length; i++){
	   		var data = waitingBox[i];
	   		if(null != data){
	   			 //校验是否可加载加载
	   			if(checkBoxLoad(scrollTop+winHeight,stageWidth,data)){
	   				buildBoxs.push(data)
	   			}else{
	   				isOver = false;
	   				leftBox.push(data);
	   			}
	   		}
	   	}
	   	for(var i = 0; i < buildBoxs.length; i++){
	   		var data = buildBoxs[i];
	   		buildOneBox(params,data);
	   	}
   		params.isOver = isOver;
	   	return leftBox;
    }
    
})(jQuery);

function scrollChange(){
	$("#websiteeditor_childchangescroller").change();
}